package charles.testproject.jlisttest;

import javax.swing.*;

import java.awt.*;

import javax.swing.border.Border;
import javax.swing.event.*; 
public class JlistTest extends JFrame {  

	private JTextArea dateAndNamePane = new JTextArea();
	private JLabel dateLabel = new JLabel();
	private JLabel nameLabel = new JLabel();
	private JLabel contentArea = new JLabel();
	/**
	 * initial method
	 */
	public void init() {
		this.dateLabel.setForeground(new Color(159,159,159));
		this.nameLabel.setForeground(new Color(159,159,159));
		this.dateLabel.setAlignmentX(SwingConstants.EAST);
		this.nameLabel.setAlignmentX(SwingConstants.WEST);
		this.dateLabel.setBackground(Color.blue);
		this.nameLabel.setBackground(Color.pink);
//		this.contentArea.setWrapStyleWord(true);
//		this.contentArea.setLineWrap(true);	
	}
	/**
	 * the data for show 
	 */
	String[] cities = {"London baby","Madrid","New York","Rome","Sydney","Toronto","Washington"};  
	DefaultListModel listModel = new DefaultListModel();  
	JList list = new JList(listModel);
	
	int currentSelection = -1;
	
	public JlistTest() {
		System.out.println("TimeStamp 1");
		/**
		 * initial the frame which contain the JList
		 */
		setLocation(400,300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		//set the Renderer
		list.setCellRenderer(new MyCellRenderer());
		
		this.list.setLayoutOrientation(JList.VERTICAL);
		
		for(int x = 0; x<cities.length; x++) {
			listModel.addElement(cities[x]);
		}
		System.out.println("TimeStamp 2");
		JScrollPane sp = new JScrollPane(list);
		sp.setPreferredSize(new Dimension(300,400));
		getContentPane().add(sp);
		pack();
		//initial the JLabels
		init();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent lse) {
				if(lse.getValueIsAdjusting() == false) {
					list.setCellRenderer(new MyCellRenderer());
					for(int x = 0; x < listModel.size(); x++) {
						listModel.setElementAt(listModel.getElementAt(x), x);
					}
				}
			}
		});
	}
	public static void main(String[] args) {
		new JlistTest().setVisible(true);
	}
	
//	class MyCellRenderer extends DefaultListCellRenderer {
	class MyCellRenderer extends JTextPane implements ListCellRenderer {
		public Component getListCellRendererComponent(JList list,
				Object value, int index, boolean isSelected, 
				boolean cellHasFocus) {
System.out.println("MyCellRenderer was called!");
//			JLabel lbl = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
//			if(list.getSelectedIndex() == index) {
//				lbl.setPreferredSize(new Dimension(100,75));
//			}
//			else {
//				lbl.setPreferredSize(new Dimension(100,20));
//			}
//		this.setText("");
//		this.setLayout(new BorderLayout());
//		this.add(dateAndNamePane,BorderLayout.NORTH);
//		dateAndNamePane.setText("");
//		dateAndNamePane.setLayout(new GridLayout(1,2));
//		dateAndNamePane.add(dateLabel);
//		dateAndNamePane.add(nameLabel);
//		this.add(contentArea, BorderLayout.SOUTH);

//		this.setText("");
//		this.setLayout(new GridLayout(2,2));
//		
//		this.add(dateLabel);
//		this.add(nameLabel);
//		this.add(contentArea);
		
		this.setBackground(Color.blue);

		this.setText("");
		GridBagLayout girdBag = new GridBagLayout();
		this.setLayout(girdBag);
		
		
		GridBagConstraints girdBagCon = new GridBagConstraints();
		girdBagCon.gridx = 0;
		girdBagCon.gridy = 0;
		girdBagCon.gridwidth = 2;
		girdBagCon.gridheight = 2;
		girdBagCon.ipadx = 20;
		girdBagCon.ipady = 20;
		girdBag.setConstraints(nameLabel,girdBagCon);
		this.add(nameLabel);
		
		girdBagCon = new GridBagConstraints();
		girdBagCon.gridx = 1;
		girdBagCon.gridy = 1;
//		girdBagCon.gridwidth = 2;
//		girdBagCon.gridheight = 2;
		girdBagCon.insets = new Insets(1,0,0,0);
		girdBagCon.ipadx = 20;
		girdBagCon.ipady = 20;
		girdBag.setConstraints(dateLabel,girdBagCon);
		this.add(dateLabel);
		
		

		girdBagCon = new GridBagConstraints();
		girdBagCon.gridx = 0;
		girdBagCon.gridy = 2;
		girdBagCon.gridwidth = 2;
		girdBagCon.gridheight = 2;
		girdBagCon.ipadx = 20;
		girdBagCon.ipady = 20;
		girdBag.setConstraints(contentArea,girdBagCon);
		this.add(contentArea);
		
		dateLabel.setText("2008-12-10");
		nameLabel.setText("Sancho_lai@163.com");
		
		contentArea.setText("come on babyfjdkfdlajdlalfjaljfldjd" + "\n" + "flldfdjfkdjfjalfjdljf;l");

		
			return this;
		}
	}
	
//	class MyCellRenderer extends JTextPane implements ListCellRenderer {
//
//		public Component getListCellRendererComponent(JList list, Object value,
//				int index, boolean isSelected, boolean cellHasFocus) {
//			
//			this.setLayout(new BorderLayout());
//System.out.println("MyCellRenderer was called! ");
//			this.add(jlb,BorderLayout.CENTER);
//			
//			jlb.setText("ZhuangBility");
//			jlb.setPreferredSize(new Dimension(100,100));
//			if(list.getSelectedIndex() == index) {
//				jlb.setPreferredSize(new Dimension(100,75));
//			}
//			else {
//				jlb.setPreferredSize(new Dimension(100,20));
//			}
//			return jlb;
//		}
//	}
}	


	
																																																																																																																																																																																																																				// this
																																																																																																																																																																																																																					// recent
																																																																																																																																																																																																																					// post,
																																																																																																																																																																																																																					// and
																																																																																																																																																																																																																					// would
																																																																																																																																																																																																																					// be
																																																																																																																																																																																																																					// better
																																																																																																																																																																																																																					// //http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=006729
																																																																																																																																																																																																																					// //comment
																																																																																																																																																																																																																					// out
																																																																																																																																																																																																																					// the
																																																																																																																																																																																																																					// above
																																																																																																																																																																																																																					// reset
																																																																																																																																																																																																																					// line,
																																																																																																																																																																																																																					// and
																																																																																																																																																																																																																					// uncomment
																																																																																																																																																																																																																					// this
																																																																																																																																																																																																																					// block
																																																																																																																																																																																																																					// if(lse.getValueIsAdjusting()
																																																																																																																																																																																																																					// ==
																																																																																																																																																																																																																					// false)
																																																																																																																																																																																																																					// {
																																																																																																																																																																																																																					// for(int
																																																																																																																																																																																																																					// x =
																																																																																																																																																																																																																					// 0; x
																																																																																																																																																																																																																					// <
																																																																																																																																																																																																																					// listModel.size();
																																																																																																																																																																																																																					// x++)
																																																																																																																																																																																																																					// {
																																																																																																																																																																																																																					// listModel.setElementAt(listModel.getElementAt(x),x);
																																																																																																																																																																																																																					// }
																																																																																																																																																																																																																					// }}});
																																																																																																																																																																																																																					// */ }
																																																																																																																																																																																																																					// public
																																																																																																																																																																																																																					// static
																																																																																																																																																																																																																					// void
																																																																																																																																																																																																																					// main(String[]
																																																																																																																																																																																																																					// args){new
																																																																																																																																																																																																																					// Testing().setVisible(true);}}class
																																																																																																																																																																																																																					// MyRenderer
																																																																																																																																																																																																																					// extends
																																																																																																																																																																																																																					// DefaultListCellRenderer{
																																																																																																																																																																																																																					// public
																																																																																																																																																																																																																					// Component
																																																																																																																																																																																																																					// getListCellRendererComponent(JList
																																																																																																																																																																																																																					// list,Object
																																																																																																																																																																																																																					// value,
																																																																																																																																																																																																																					// int
																																																																																																																																																																																																																					// index,boolean
																																																																																																																																																																																																																					// isSelected,boolean
																																																																																																																																																																																																																					// cellHasFocus)
																																																																																																																																																																																																																					// {
																																																																																																																																																																																																																					// JLabel
																																																																																																																																																																																																																					// lbl
																																																																																																																																																																																																																					// =
																																																																																																																																																																																																																					// (JLabel)super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
																																																																																																																																																																																																																					// if(list.getSelectedIndex()
																																																																																																																																																																																																																					// ==
																																																																																																																																																																																																																					// index)
																																																																																																																																																																																																																					// lbl.setPreferredSize(new
																																																																																																																																																																																																																					// Dimension(100,75));
																																																																																																																																																																																																																					// else
																																																																																																																																																																																																																					// lbl.setPreferredSize(new
																																																																																																																																																																																																																					// Dimension(100,20));
																																																																																																																																																																																																																					// return
																																																																																																																																																																																																																					// lbl;
																																																																																																																																																																																																																					// }}
